﻿/* Copyright © 2012 Gargol, Gargol at gbot dot org
   Based on CoreDC Copyright 2007 © Johan Pettersson, Fredrik Johansson, Michael Starberg, Christian Epstein.
 
This file is part of ADCCore.

ADCCore is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

ADCCore is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ADCCore.  If not, see <http://www.gnu.org/licenses/>. */
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;


namespace ADCCore.Classes
{
    public class SearchReply
    {
        public SearchReply()
        {
            this.User = new User();
            this.IsActive = false;
            this.FilePath = String.Empty;
            this.FileSize = 0;
            this.SlotsFree = 0;
            this.SlotsTotal = 0;
            this.TTH = String.Empty;
            this.HubIP = new IPAddress(0);
            this.HubPort = 0;
            this.Token = String.Empty;
        }
        /// <summary>
        /// The token the searchreply originated with
        /// </summary>
        public String Token { get; set; }

        /// <summary>
        /// The user the searchreply originates from
        /// </summary>
        public User User { get; set; }

        /// <summary>
        /// Signals whether this is an active searchreply or passive
        /// </summary>
        public bool IsActive { get; set; }

        /// <summary>
        /// The path and filename of the file in the users share
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// The size of the file
        /// </summary>
        public Int64 FileSize { get; set; }

        /// <summary>
        /// Number of free slots of the user
        /// </summary>
        public int SlotsFree { get; set; }

        /// <summary>
        /// Total number of slots of the user
        /// </summary>
        public int SlotsTotal { get; set; }

        /// <summary>
        /// The Tiger Tree Hash of the file
        /// </summary>
        public string TTH { get; set; }

        /// <summary>
        /// The IP-address of the hub the user is connected to
        /// </summary>
        public IPAddress HubIP { get; set; }

        /// <summary>
        /// The port of the hub the user is connected to
        /// </summary>
        public int HubPort { get; set; }

        /// <summary>
        /// Formats the variables into a proper DC-protocol searchreply
        /// </summary>
        public override string ToString()
        {
            //Format: $SR <ownname> <filenamewithpath)<filesizeinbytes>
            // <current openslots>/<allopenslots><hubname> (<hubip>:<hubport>)<targetuser> 
            //RES 
            /*  FN 	Full filename including path in share
                SI 	Size, in bytes
                SL 	Slots currently available
                TO
             */
            StringBuilder sr = new StringBuilder();

            
            /*sr.Append(Convert.ToChar(5));
            sr.AppendFormat("{0} {1}/{2}", FileSize, SlotsFree, SlotsTotal);
            sr.Append(Convert.ToChar(5));
            sr.AppendFormat("TTH:{0} ({1}:{2})", TTH, HubIP, HubPort);
            sr.Append(Convert.ToChar(5));*/
            sr.Append("SI" + FileSize);
            sr.Append(" SL" + SlotsFree);
            sr.Append(" FN" + DcClient.DcEncode(FilePath));
            sr.Append(" TR" + TTH);
            if (Token != "")
                sr.Append(" TO" + Token);

            return sr.ToString();
        }
    }
}
